package org.cid54.bukkit.model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Cid54
 */
public class GateDao {

    public static final GateDao instance = new GateDao();
    private Map<String, Cid54Gate> gates;
    private static final Object fileLock = new Object();

    private GateDao() {
        File f = new File("gates.csv");
        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    private String marshall(Cid54Gate gate) {

        StringBuilder result = new StringBuilder();

        result.append(gate.getName().toLowerCase()).append(";");
        result.append(gate.getX()).append(";");
        result.append(gate.getY()).append(";");
        result.append(gate.getZ());

        return result.toString();
    }

    private Cid54Gate unMarshall(String line) {

        String[] pieces = line.split(";");

        Cid54Gate gate = new Cid54Gate();
        gate.setName(pieces[0]);
        gate.setX(Integer.parseInt(pieces[1]));
        gate.setY(Integer.parseInt(pieces[2]));
        gate.setZ(Integer.parseInt(pieces[3]));

        return gate;
    }

    private void writeGates() {
        synchronized (fileLock) {
            try {

                FileWriter fw = new FileWriter("gates.csv", false);
                BufferedWriter bw = new BufferedWriter(fw);

                for (Cid54Gate gate : this.gates.values()) {
                    bw.write(this.marshall(gate) + "\n");
                }

                bw.flush();
                bw.close();

            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    private void loadGates() {
        synchronized (fileLock) {
            try {

                this.gates = new HashMap<String, Cid54Gate>();

                FileReader fr = new FileReader("gates.csv");
                BufferedReader br = new BufferedReader(fr);

                String line = br.readLine();

                while (line != null) {

                    Cid54Gate gate = this.unMarshall(line);
                    this.gates.put(gate.getName(), gate);
                    line = br.readLine();
                }

                br.close();
                fr.close();

            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public List<Cid54Gate> getAllGates() {
        if (this.gates == null) {
            this.loadGates();
        }

        List<Cid54Gate> resultL = new ArrayList<Cid54Gate>(this.gates.values());

        Collections.sort(resultL, new Comparator<Cid54Gate>() {

            public int compare(Cid54Gate o1, Cid54Gate o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        return resultL;
    }

    public Cid54Gate getGateByName(String name) {
        if (this.gates == null) {
            this.loadGates();
        }

        return this.gates.get(name.toLowerCase());
    }

    public void addGate(Cid54Gate gate) {
        if (this.gates == null) {
            this.loadGates();
        }
        this.gates.put(gate.getName().toLowerCase(), gate);

        this.writeGates();

    }

    public void deleteGate(Cid54Gate gate) {
        if (this.gates == null) {
            this.loadGates();
        }

        this.gates.remove(gate.getName().toLowerCase());

        this.writeGates();
    }
}
